package com.jp.algorithm.stack;

import java.util.Stack;

public class TemperatureDaily {

    public int[] getArray(int[] array){

        if( array==null || array.length == 0)
            return null;
        int[] newArray = new int[array.length];

        Stack<Node> stack = new Stack<>();
        for(int i=0; i<array.length; i++){
            if(i==0){
                stack.push(new Node(i,array[i]));
                newArray[i]=0;
            }else{
                while(!stack.isEmpty()){
                    if(array[i]>stack.peek().t){
                        Node node = stack.pop();
                        newArray[node.index]=i-node.index;
                    }else{
                        stack.push(new Node(i,array[i]));
                        newArray[i]=0;
                        break;
                    }
                }
            }
        }
        return newArray;

    }
    class Node{

        int index;
        int t;
        public  Node(int index, int t){
            this.index = index;
            this.t = t;
        }
    }
}
